package com.work.xxzl.module.fragment;

import android.os.Handler;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.work.xxzl.R;
import com.work.xxzl.adapter.CommonAdapter;
import com.work.xxzl.adapter.CommonFragmentPagerAdapter;
import com.work.xxzl.adapter.ViewHolder;
import com.work.xxzl.base.BaseFragmentForMain;
import com.work.xxzl.module.pagehome.CityPickerActivity;
import com.work.xxzl.module.pagehome.LoveWallFragment;
import com.work.xxzl.module.pagehome.MakeFriendsFragment;
import com.work.xxzl.module.pagehome.MatrimoniaFragment;
import com.work.xxzl.module.pagehome.SuggestFragment;
import com.work.xxzl.module.pagehome.TeacherFragment;
import com.work.xxzl.utils.UIUtils;
import com.work.xxzl.utils.ViewUtils;
import com.work.xxzl.view.CommonPopupWindow;
import com.zaaach.citypicker.CityPicker;
import com.zaaach.citypicker.adapter.OnPickListener;
import com.zaaach.citypicker.model.City;
import com.zaaach.citypicker.model.HotCity;
import com.zaaach.citypicker.model.LocateState;
import com.zaaach.citypicker.model.LocatedCity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * @author WZ 2018/12/3
 * 首页
 */
public class HomePageFragment extends BaseFragmentForMain {
    @BindView(R.id.tablayout)
    TabLayout tablayout;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.tv_location)
    TextView tvLocation;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.iv_add)
    ImageView ivAdd;
    private List<Fragment> fragments = new ArrayList<>();
    private String tabTitles[] = {"推荐", "交友", "征婚", "幸福导师", "恋爱墙"};
    private CommonFragmentPagerAdapter adapter;
    private List<String> listdata = new ArrayList<>();
    private CommonPopupWindow commonPopupWindow;
    private CityPicker citypicker;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_homepage_content;
    }

    @Override
    protected void initView() {
        setViewPagerTablayout();
        ViewUtils.setOnClickListeners(this, tvLocation, tvSearch, ivAdd);
    }

    @Override
    protected void initData() {
        super.initData();
        setPopupWindow();
        //设置城市选择
        setShowcity();
    }

    //Tablayout和ViewPager
    private void setViewPagerTablayout() {
        fragments.add(new SuggestFragment());
        fragments.add(new MakeFriendsFragment());
        fragments.add(new MatrimoniaFragment());
        fragments.add(new TeacherFragment());
        fragments.add(new LoveWallFragment());
        adapter = new CommonFragmentPagerAdapter(getActivity().getSupportFragmentManager(), fragments, tabTitles);
        viewpager.setAdapter(adapter);
        tablayout.setupWithViewPager(viewpager);
        viewpager.setCurrentItem(0);
        for (int i = 0; i < tabTitles.length; i++) {
            TextView title = (TextView) (((LinearLayout) ((LinearLayout) tablayout.getChildAt(0)).getChildAt(i)).getChildAt(1));
            //选中字体样式
            if(i==0){
                title.animate().scaleX(1.0f);
                title.animate().scaleY(1.0f);
            }else {
                title.animate().scaleX(0.8f);
                title.animate().scaleY(0.8f);
            }
        }
        tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewpager.setCurrentItem(tab.getPosition());
                TextView title = (TextView) (((LinearLayout) ((LinearLayout) tablayout.getChildAt(0)).getChildAt(tab.getPosition())).getChildAt(1));
                //选中字体样式
                title.animate().scaleX(1.0f);
                title.animate().scaleY(1.0f);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                TextView title = (TextView) (((LinearLayout) ((LinearLayout) tablayout.getChildAt(0)).getChildAt(tab.getPosition())).getChildAt(1));
                //未选中字体样式
                title.animate().scaleX(0.8f);
                title.animate().scaleY(0.8f);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }


    @Override
    protected void processClick(View v) {
        super.processClick(v);
        switch (v.getId()) {
            case R.id.tv_location:
                //选择地址
                citypicker.show();
//                UIUtils.startActivity(CityPickerActivity.class);
                break;
            case R.id.tv_search:
                //搜索
                break;
            case R.id.iv_add:
                //加号
                backgroundAlpha(0.7f);
                commonPopupWindow.showAsDropDown(ivAdd, -UIUtils.dip2px(110), 5);
                break;

        }
    }

    //加号按钮弹框
    private void setPopupWindow() {
        if (listdata.size() == 0) {
            listdata.add("二维码");
            listdata.add("扫一扫");
            listdata.add("充值");
            listdata.add("推荐");
            listdata.add("置顶");
        }
        if (commonPopupWindow == null) {
            commonPopupWindow = new CommonPopupWindow(getActivity(), R.layout.layout_listview);
            commonPopupWindow.setWidth(UIUtils.dip2px(150));
            commonPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.messagebg));
        }
        View view = commonPopupWindow.getMenuView();
        ListView listView = view.findViewById(R.id.lv_listview);
        listView.setDividerHeight(2);
        listView.setPadding(20, 0, 0, 0);
        listView.setAdapter(new CommonAdapter<String>(getActivity(), listdata, R.layout.item_textview_withimage) {
            @Override
            public void convert(ViewHolder helper, String item, int position) {
                helper.setText(R.id.textview, item);
                switch (position) {
                    case 0:
                        helper.setImage(R.id.imageview, R.mipmap.qrcode);
                        break;
                    case 1:
                        helper.setImage(R.id.imageview, R.mipmap.saoyisao);
                        break;
                    case 2:
                        helper.setImage(R.id.imageview, R.mipmap.investmoney);
                        break;
                    case 3:
                        helper.setImage(R.id.imageview, R.mipmap.sharepeople);
                        break;
                    case 4:
                        helper.setImage(R.id.imageview, R.mipmap.totop);
                        break;
                }
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                commonPopupWindow.dismiss();
                switch (position) {
                    case 0:
                        //二维码
                        break;
                    case 1:
                        //扫一扫

                        break;
                    case 2:
                        //充值

                        break;
                    case 3:
                        //推荐

                        break;
                    case 4:
                        //置顶

                        break;
                }
            }
        });
        commonPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1.0f);
            }
        });
    }


    private void setShowcity() {
        List<HotCity> hotCities = new ArrayList<>();
        hotCities.add(new HotCity("北京", "北京", "101010100"));
        hotCities.add(new HotCity("上海", "上海", "101020100"));
        hotCities.add(new HotCity("广州", "广东", "101280101"));
        hotCities.add(new HotCity("深圳", "广东", "101280601"));
        hotCities.add(new HotCity("杭州", "浙江", "101210101"));
        citypicker = CityPicker.getInstance()
                .setFragmentManager(getActivity().getSupportFragmentManager())  //此方法必须调用
                .enableAnimation(true)  //启用动画效果
                .setAnimationStyle(R.style.popwindowdialog_anim_style)  //自定义动画
                .setLocatedCity(new LocatedCity("郑州", "河南", "101210101")) //APP自身已定位的城市，默认为null（定位失败）
                .setHotCities(hotCities)  //指定热门城市
                .setOnPickListener(new OnPickListener() {
                    @Override
                    public void onPick(int position, City data) {
                        if (data != null) {
                            tvLocation.setText(data.getName());
//                            UIUtils.showToast(data.getName());
                        }
                    }

                    @Override
                    public void onLocate() {
                        //开始定位，这里模拟一下定位
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                //定位完成之后更新数据
                                CityPicker.getInstance()
                                        .locateComplete(new LocatedCity("深圳", "广东", "101280601"), LocateState.SUCCESS);
                            }
                        }, 2000);
                    }
                });
    }
}
